import IoreDis from 'ioredis'
import { Models } from '../common/typings/model'
import config from '../config/Config'
export default class RedisClient {
  public static readonly redis = new IoreDis({
    port: 6379, // Redis port
    host: '1.116.40.155', // Redis host
    password: 'admin',
    db: 0,
  })

  /**
   * 获取token的值
   * @param key
   * @returns
   */
  public static getTokenValue(key: string): Promise<Models.Result> {
    return new Promise((resolve) => {
      const redis = RedisClient.redis
      redis.select(3).then(() => {
        redis.get(key).then((res) => {
          const result: Models.Result = {
            msg: 'ok',
            state: 1,
            results: res,
            fields: [],
          }
          resolve(result)
        })
      })
    })
  }

  /**
   * 保存token
   * @param key
   * @param value
   * @returns
   */
  public static saveToken(key: string, value: IoreDis.ValueType): Promise<Models.Result> {
    return new Promise((resolve) => {
      const redis = RedisClient.redis
      redis.select(3).then(() => {
        redis.setex(key, config.security.expiresIn, value).then((res) => {
          const result: Models.Result = {
            msg: 'ok',
            state: 1,
            results: res,
            fields: [],
          }
          resolve(result)
        })
      })
    })
  }

  /**
   * 删除token
   * @param key
   * @returns
   */
  public static deleteToken(key: string): Promise<Models.Result> {
    return new Promise((resolve) => {
      const redis = RedisClient.redis
      redis.select(3).then(() => {
        redis.del(key).then((res) => {
          const result: Models.Result = {
            msg: 'ok',
            state: 1,
            results: res,
            fields: [],
          }
          resolve(result)
        })
      })
    })
  }

  /**
   * 更新权限
   * @param roleId
   * @param values
   * @returns
   */
  public static addRoles(roleId: string, values: IoreDis.ValueType[]): Promise<Models.Result> {
    return new Promise((resolve) => {
      const redis = RedisClient.redis
      redis.select(1).then(() => {
        redis.sadd(roleId, ...values).then((res) => {
          const result: Models.Result = {
            msg: 'ok',
            state: 1,
            results: res,
            fields: [],
          }
          resolve(result)
        })
      })
    })
  }

  /**
   * 更新权限
   * @param roleId
   * @param obj
   * @returns
   */
  public static updateRoles(roleId: string, obj: Map<string, IoreDis.ValueType>): Promise<Models.Result> {
    return new Promise((resolve) => {
      const redis = RedisClient.redis
      redis.select(1).then(async () => {
        await redis.del(roleId)
        redis.hmset(roleId, obj).then((res) => {
          const result: Models.Result = {
            msg: 'ok',
            state: 1,
            results: res,
            fields: [],
          }
          resolve(result)
        })
      })
    })
  }

  /**
   * 将一个值插入到已存在的列表头部
   * @param listName
   * @param value
   * @returns
   */
  public static lpush(listName: string, value: IoreDis.ValueType[]): Promise<Models.Result> {
    return new Promise((resolve) => {
      const redis = RedisClient.redis
      redis.lpush(listName, ...value)
      const result: Models.Result = {
        msg: 'ok',
        state: 1,
        results: [],
        fields: [],
      }
      resolve(result)
    })
  }

  /**
   * 读取数组
   * @param listName
   * @param start
   * @param stop
   * @returns
   */
  public static lrange(listName: string, start: number, stop: number): Promise<Models.Result> {
    return new Promise((resolve) => {
      const redis = RedisClient.redis
      redis.lrange(listName, start, stop).then((res: string[]) => {
        const result: Models.Result = {
          msg: 'ok',
          state: 1,
          results: res,
          fields: [],
        }
        resolve(result)
      })
    })
  }

  /**
   * 向集合添加一个或多个成员
   * @param setName
   * @param value
   * @returns
   */
  public static sadd(setName: string, value: IoreDis.ValueType[]): Promise<Models.Result> {
    return new Promise((resolve) => {
      const redis = RedisClient.redis
      redis.sadd(setName, ...value).then((res: number) => {
        const result: Models.Result = {
          msg: 'ok',
          state: 1,
          results: res,
          fields: [],
        }
        resolve(result)
      })
    })
  }

  public static sismember(setName: string, value: string): Promise<Models.Result> {
    return new Promise((resolve) => {
      const redis = RedisClient.redis
      redis.sismember(setName, value).then((res: IoreDis.BooleanResponse) => {
        const result: Models.Result = {
          msg: 'ok',
          state: 1,
          results: res,
          fields: [],
        }
        resolve(result)
      })
    })
  }
}

setInterval(() => {
  RedisClient.redis.exists('0')
}, 15000)

// const RedisClient = new RedisClient()
// export default RedisClient
